// 注入到页面上下文的脚本，劫持 history 方法和监听 url 变化
(function() {
    let lastUrl = window.location.href;
    function emitUrlChange() {
        const currentUrl = window.location.href;
        if (currentUrl !== lastUrl) {
            lastUrl = currentUrl;
            window.dispatchEvent(new CustomEvent('AvaUrlChange', { detail: { url: currentUrl } }));
        }
    }
    ["pushState", "replaceState"].forEach(type => {
        const orig = history[type];
        history[type] = function () {
            const result = orig.apply(this, arguments);
            emitUrlChange();
            return result;
        };
    });
    window.addEventListener('popstate', emitUrlChange);
    window.addEventListener('hashchange', emitUrlChange);
})();
